package com.happystreet.config
{
	import com.happystreet.mission.MissionData;
	import com.happystreet.mission.MissionOrderGoodTemplate;
	import com.happystreet.mission.MissionSatisfyCustomerTemplate;
	import com.happystreet.mission.MissionTakePictureTemplate;
	import com.happystreet.mission.MissionTemplate;
	import com.happystreet.mission.MissionUpgradeTruckTemplate;
	import com.happystreet.mission.MissionXMLTemplate;

	/**
	 *任务配置 
	 * @author Administrator
	 * 
	 */	
	public class MissionsConfig
	{
		private static var everyMissionTable:Array;
		
		public function MissionsConfig()
		{
			
		}
		
		/**
		 *解析Mission配置 
		 * @param config
		 * 
		 */		
		public static function setMissionsConfig(config:String):void
		{
			var mission:XML;
			var missionTemplate:MissionTemplate;
			var missionData:MissionData;
			var missionConfig:XML = new XML(config);
			var missionList:XMLList = missionConfig..mission;
			everyMissionTable = new Array();
			for each (mission in missionList)
			{
				missionTemplate = new MissionXMLTemplate(mission);
				missionData = new MissionData(missionTemplate);
				everyMissionTable.push(missionData);
			}
			missionConfig = null;
			missionList = null;
		}
		
		/**
		 *从初始化响应获取任务更新 
		 * @param missionList		任务列表
		 * @return 
		 * 
		 */		
		public static function getUpdatedMissionsFromInitResponse(missionList:Array):Array
		{
			var missionObj:Object;
			var missionData:MissionData;
			var updatedList:Array = new Array();
			if(missionList==null){
				return updatedList
			}
			
			for each (missionObj in missionList)
			{
				missionData = MissionsConfig.getMissionById(missionObj.id);
				if (missionData == null)
				{
					missionData = createConfigurableMissionFromId(missionObj.id, missionObj.goals);
					if (missionData == null)
					{
						trace("Error: There's no mission with ID=" + missionObj.id + ". Check missions.xml or flush your cache");
						continue;
					}
					everyMissionTable.push(missionData);
				}
				missionData.missionProgress = missionObj.goals;
				updatedList.push(missionData);
			}
			
			return updatedList;
		}
		
		/**
		 *通过任务id获取任务数据 
		 * @param id
		 * @return 
		 * 
		 */		
		public static function getMissionById(id:String):MissionData
		{
			var missionData:MissionData;
			for each (missionData in everyMissionTable)
			{
				if (missionData.id == id)
				{
					return missionData;
				}
			}
			return null;
		}
		
		/**
		 *根据任务id组装任务 
		 * @param id
		 * @param goals
		 * @return 
		 * 
		 */		
		public static function createConfigurableMissionFromId(id:String,goals:Object):MissionData
		{
			var pictureTemplate:MissionTakePictureTemplate;
			var upgradeTruckTemplate:MissionUpgradeTruckTemplate;
			var satisfyTemplate:MissionSatisfyCustomerTemplate;
			var orderGoodsTemplate:MissionOrderGoodTemplate;
			
			trace("Creating a configurable mission (name: " + id + ")");
			if(id.indexOf(MissionData.TYPE_TAKE_PICTURE)==0){
				pictureTemplate = new MissionTakePictureTemplate(id);
				return new MissionData(pictureTemplate);
			}
			
			if(id.indexOf(MissionData.TYPE_UPGRADE_TRUCK)==0){
				upgradeTruckTemplate = new MissionUpgradeTruckTemplate(id);
				return new MissionData(upgradeTruckTemplate);
			}
			
			if(id.indexOf(MissionData.TYPE_SATISFY_CUSTOMER)==0){
				satisfyTemplate = new MissionSatisfyCustomerTemplate(id,goals);
				return new MissionData(satisfyTemplate);
			}
			
			if(id.indexOf(MissionData.TYPE_ORDER_GOOD)==0)
			{
				orderGoodsTemplate = new MissionOrderGoodTemplate(id);
				return new MissionData(orderGoodsTemplate);
			}
			
			return null;
		}
	}
}